package com.gegao.learndemo.CustomView;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * 弧度View
 * Created by liuchenghui on 2015/12/15.
 */
public class ProgressView extends View implements Runnable {

    private int mMeasureWidth;
    private int mMeasureHeight;

    private int mCircleXY;
    private float mCircleRadius;
    private Paint mCirClePaint;

    private RectF mArcRectF;
    private float mSweepAngle;
    private float mSweepValue = 0;
    private Paint mArcPaint;

    private String mShowText = "Hello Circle";

    public ProgressView(Context context) {
        super(context);
    }

    public ProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mMeasureWidth = MeasureSpec.getSize(widthMeasureSpec);
        mMeasureHeight = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(mMeasureWidth, mMeasureHeight);
        init();
    }

    private void init() {
        int length;
        if (mMeasureHeight >= mMeasureWidth) {
            length = mMeasureWidth;
        } else {
            length = mMeasureHeight;
        }

        mCircleXY = length / 2;
        mCircleRadius = (float) (length / 4);
        mCirClePaint = new Paint();
        mCirClePaint.setAntiAlias(true);
        mCirClePaint.setColor(Color.RED);

        mArcRectF = new RectF(
                (float) (length * 0.1),
                (float) (length * 0.1),
                (float) (length * 0.9),
                (float) (length * 0.9));
        mSweepAngle = (mSweepValue / 100f) * 360f;
        mArcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mArcPaint.setColor(Color.RED);
        mArcPaint.setStrokeWidth((float) (length * 0.1));
        mArcPaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(mCircleXY, mCircleXY, mCircleRadius, mCirClePaint);
        canvas.drawArc(mArcRectF, 270, mSweepAngle, false, mArcPaint);
//        canvas.drawText(mShowText, 0, mShowText.length(),mCircleXY, mCircleXY + (50 / 4), new Paint());
    }

    @Override
    public void run() {
        while (true) {
            if (mSweepValue <+ 100) {
                mSweepValue++;
            } else {
                mSweepValue = 0;
            }
            mSweepAngle = (mSweepValue / 100f) * 360f;
            postInvalidate();
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
